{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "13\n",
      "21\n",
      "34\n",
      "55\n"
     ]
    }
   ],
   "source": [
    "## 生成器函数：函数里只要有yield关键字\n",
    "\n",
    "def gen_func():\n",
    "    yield 1\n",
    "    yield 2\n",
    "    yield 3\n",
    "## 惰性求值，延迟求值提供了可能\n",
    "\n",
    "## 斐波那契数列实现\n",
    "def fib(index):\n",
    "    re_list = []\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < index:\n",
    "        re_list.append(b)\n",
    "        a, b = b, a + b\n",
    "        n += 1\n",
    "    return re_list\n",
    "\n",
    "## 斐波那契数列 生成器实现\n",
    "def gen_fib(index):\n",
    "    re_list = []\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < index:\n",
    "        # 输出新值\n",
    "        yield b\n",
    "        a, b = b, a + b\n",
    "        n += 1\n",
    "    return re_list\n",
    "\n",
    "def func():\n",
    "    return 1\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    result = fib(10)\n",
    "    print(result)\n",
    "    ## 生成器斐波那契\n",
    "    for data in gen_fib(10):\n",
    "        print(data)\n",
    "    \n",
    "    # 生成器对象，python编译字节码的时候就产生了\n",
    "#     gen = gen_func()\n",
    "#     for value in gen:\n",
    "#         print(value)\n",
    "        \n",
    "#     re = func()\n",
    "#     pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
